/**
 * @(#) MyProfileMediator.as
 *
 * @date		2009. 02. 12.
 * @version		1.0
 * @author		Shin, Yunwoo
 * @description	본인 인사기록 조회 - Mediator
 * 				본인의 인사기록을 조회하는 Presentation Logic을 관리 
 */
package com.skt.common_hr.myprofile_stc.view
{
	import com.skt.common_hr.common.*;
	import com.skt.common_hr.common.components.CommonAlert;
	import com.skt.common_hr.common.puremvc.OverViewMediator;
	import com.skt.common_hr.myprofile_stc.MyProfileFacade;
	import com.skt.common_hr.myprofile_stc.model.*;
	import com.skt.common_hr.myprofile_stc.model.vo.CareerVO;
	import com.skt.common_hr.myprofile_stc.model.vo.FamilyVO;
	import com.skt.common_hr.myprofile_stc.model.vo.PhonesVO;
	import com.skt.common_hr.myprofile_stc.model.vo.PrivateVO;
	import com.skt.common_hr.myprofile_stc.model.vo.ScholarshipVO;
	import com.skt.common_hr.myprofile_stc.view.components.OpinionView;
	import com.skt.common_hr.myprofile_stc.view.components.OverView;
	import com.skt.common_hr.myprofile_stc.view.components.TabSheet1;
	import com.skt.common_hr.myprofile_stc.view.components.TabSheeta_Stc;
	
	import flash.events.Event;
	
	import mx.collections.ArrayCollection;
	import mx.managers.PopUpManager;
	
	import org.puremvc.as3.multicore.interfaces.INotification;
	

	public class MyProfileMediator extends OverViewMediator
	{
		public static const NAME:String = "MyProfileMediator"; 
		private const bundle:String = "MyProfile";
		
		private var _status_update:Boolean = false;
		
		/**
		* 생성자
		* 
		* @param
		* @return void
		*/
        public function MyProfileMediator( viewComponent:Object )
        {
            super( NAME, viewComponent );
        }
 		





		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Override functions of CommonMediator
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		* View를 초기화 한다.
		*	- View에 기본사항을 출력하고 data를 load
		* @param
		* @return void
		*/
 		override protected function initializeView():void      
 		{
 			//setStatusMessage(AppModuleManager.MODULE_KEY_MYPROFILE, );
 			commonFacade.setStatusMessage("STATUS_LOADING_MYPROFILE");
 			setCountry();
			view.addEventListener("CLICK_MODIFY_SCREEN", onClickModifyScreen);
			view.addEventListener(TabSheet1.ChangePhoneNumber, onChangedPhoneNumber);
			view.addEventListener(TabSheet1.ChangeAddress, onChangeAddress);
			view.addEventListener(OverView.RELOAD_DATE, mainDataReflesh);
			view.addEventListener(OverView.TAB_CHANGE, onTabChange);
			
			view.tsa.addEventListener(TabSheeta_Stc.EVENT_VIEW_OPINION, onViewOpinion);
			clearView();
			trace("initialized mediator");
 		}
 		
		override protected function activatedView():void
		{
			clearView();
			var passWordMediator:PassWordCheckMediator = facade.retrieveMediator(PassWordCheckMediator.NAME) as PassWordCheckMediator;
			passWordMediator.execute();
		}

		/**
		* View에 표현된 데이터들을 삭제한다.
		*	- view가 sleep 으로 들어갈때 실행됨..
		* @param
		* @return void
		*/
 		override protected function clearView():void
 		{
 			// silver 
 			if(Variables.company == 'SAC') {
 				view.vsCompensation.selectedIndex = 1;	
 				loadProfile();
 			} else {
 				view.vsCompensation.selectedIndex = 0;
 			}
 			
 			view.tabNavi.selectedIndex = 0;
 			tab1Init();
 			
 			view.checkView.initNewPass.text = "";
 			view.checkView.initReconfirmPass.text = "";
 			
 			view.checkView.regno1.text = "";
 			view.checkView.regno2.text = "";
			//loadProfile();
 		}
 		
 		
 		
		private function setCountry():void
		{
			if(Variables.country!="CN") {
				view.ts1cn.tabVisible = false;
				view.ts5cn.tabVisible = false;
				view.tsCn01.tabVisible = false;
			}
			else {
				view.ts1.tabVisible = false;
				view.ts5.tabVisible = false;
				view.tabNavi.selectedIndex=1;
			}
			
			if(Variables.company == 'STC') {
				view.ts11.tabVisible = true;
				view.ts12.tabVisible = true;
				view.tsa.tabVisible = true;
				//view.ts2.tabVisible = true;
			}
			//ace 
			else {
				//view.ts2.tabVisible = false;
				
				view.ts11.tabVisible = false;
				view.ts12.tabVisible = false;				
				view.tsa.tabVisible = false;
			}
		}


		private var modiMedidator:ModifyProfileMediator = null;
		private function onClickModifyScreen(e:Event):void
		{
			view.currentState = "stateModify";
			
			if(modiMedidator == null) {
				modiMedidator = new ModifyProfileMediator(view);
				facade.registerMediator(modiMedidator);
			}
			
		}

		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Process MVC Messages
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		* 본 Class에서 처리해야할 Noti들을 등록한다.
		* 
		* @param
		* @return void
		*/
		override public function listNotificationInterests():Array 
		{
			return basicInterestMessages.concat([
					MyProfileFacade.LOAD_PROFILE_COMPLETE,
					MyProfileFacade.LOAD_PROFILE_FAILED,
					MyProfileFacade.LOAD_PROFILE_DETAIL_COMPLETE,
					MyProfileFacade.LOAD_PROFILE_DETAIL_FAILED,
					/*
					MyProfileFacade.SAVE_ADDRESS_COMPLETE,
					MyProfileFacade.SAVE_ADDRESS_FAILED,
					*/
					MyProfileFacade.SAVE_PHONES_COMPLETE,
					MyProfileFacade.SAVE_PHONES_FAILED,
					MyProfileFacade.CANCEL_ADDRESS_INPUT,
					MyProfileFacade.OK_ADDRESS_INPUT,
					PassWordCheckMediator.VALID_PASSWORD,
					FamilyProxy.LOAD_FAMILY_COMPLETE,
					FamilyProxy.LOAD_FAMILY_FAILED,
					ModifyProfileMediator.END_OF_MODIFY
					]);
		}
		

		/**
		* Noti별로 처리한다.
		* 
		* @param
		* @return void
		*/
		override public function handleNotification(note:INotification):void
		{
			super.handleNotification(note);
			
			switch(note.getName()) 
			{
				case PassWordCheckMediator.VALID_PASSWORD:
					view.vsCompensation.selectedIndex = 1;
 					loadProfile();
					break;
				case MyProfileFacade.LOAD_PROFILE_COMPLETE:
					onLoadProfile(note.getBody() as ProfileProxy);
					break;
				case MyProfileFacade.LOAD_PROFILE_FAILED:
					onFailedProfile(note.getBody() as ProfileProxy);
					break;
				case MyProfileFacade.LOAD_PROFILE_DETAIL_COMPLETE:
					onLoadDetail(note.getBody() as ProfileDetailProxy);
					break;
				case MyProfileFacade.LOAD_PROFILE_DETAIL_FAILED:
					onFailedDetail(note.getBody() as ProfileDetailProxy);
					break;
				case CareerProxy.LOAD_CAREER_COMPLETE:
					onLoadCareer(note.getBody() as CareerProxy);
					break;
				case FamilyProxy.LOAD_FAMILY_COMPLETE:
					onLoadFamily(note.getBody() as FamilyProxy);
					break;
				case ScholarshipProxy.LOAD_SCHOLARSHIP_COMPLETE:
					onLoadScholarship(note.getBody() as ScholarshipProxy);
					break;
				/*
				case MyProfileFacade.SAVE_ADDRESS_COMPLETE:
					onUpdatedAddress(note.getBody() as AddressProxy);
					break;
				case MyProfileFacade.SAVE_ADDRESS_FAILED:
					onFailedUpdateAddress(note.getBody() as AddressProxy);
					break;
				*/
				case MyProfileFacade.SAVE_PHONES_COMPLETE:
					onUpdatedPhones(note.getBody() as PhonesProxy);
					break;
				case MyProfileFacade.SAVE_PHONES_FAILED:
					onFailedUpdatePhones(note.getBody() as PhonesProxy);
					break;
				
				case MyProfileFacade.OK_ADDRESS_INPUT:
					_status_update = true;
					loadProfile();
 					commonFacade.setStatusMessage("STATUS_UPDATED_ADDRESS");
				case MyProfileFacade.CANCEL_ADDRESS_INPUT:
					if(Variables.country!="CN") {
						view.ts1.currentState = "";
						view.ts1.borderSides = "";
					}
					else {
						view.ts1cn.currentState = "";
						view.ts1cn.borderSides = "";
					}
					break;
				case ModifyProfileMediator.END_OF_MODIFY:
					loadProfile();
					break;
				default:
					//trace(this.instanceID+"에서 처리 안된 Noti가 검출됨 : " + note.getBody());
					break;
			}
		}


		/**
		 * Proxy에서 DataLoad를 완료한 후 호출됨
		 *     - load된 데이터를 화면에 출력
		 * @param
		 * @return void
		 */
 		private function onLoadProfile(proxy:ProfileProxy):void
 		{
 			view.dataProvider = proxy.profile;
 			if(Variables.country != "CN") {
 				view.ts1.currentState = "";
 			}
 			else {
 				view.ts1cn.currentState = "";
 			}
 			trace(view.dataProvider.PHOTO_URL);
 			commonFacade.setStatusMessage("STATUS_LOADED_MYPRFILLE");
 		}
 		
 		private function onFailedProfile(proxy:ProfileProxy):void
 		{
 			view.dataProvider = null;
 			view.ts1.currentState = "";
 			CommonAlert.showMessage(bundle,
 									"ALERT_FAILED_LOAD_PROFILE", [proxy.retText],
 									"TITLE_INFO", view);
 			commonFacade.setStatusMessage("");
 		}
 		
 		private function onLoadDetail(proxy:ProfileDetailProxy):void
 		{
 			if(Variables.country!="CN") {
	 			view.ts1.dataProvider = proxy.privateDetail;
	 			view.ts5.dataProvider = proxy.family;
 			}
 			else {
	 			view.ts1cn.dataProvider = proxy.privateDetail;
	 			view.ts5cn.dataProvider = proxy.family;
	 			view.tsCn01.dataProvider = proxy.cn01;
 			}
 			view.ts2.dataProvider = proxy.officalAnn;
 			view.ts30.dataProvider = proxy.skCareer;
 			view.ts3.dataProvider = proxy.career;
 			view.ts4.dataProvider = proxy.scholarship;
 			view.ts7.dataProvider = proxy.education;
 			view.ts8.dataProvider = proxy.language;
 			view.ts9.dataProvider = proxy.license;
 			view.tsa.dataProvider = proxy.health;
 			
 			view.ts11.dataProvider = proxy.award;
 			view.ts12.dataProvider = proxy.disciplinary;
 			
 			if(_status_update) {
 				_status_update = false;
 			}
 			else 
				MyProfileFacade(facade).setStatusMessage("STATUS_LOADED_MYPRFILLE");
 		}

 		private var opinionView:OpinionView = null;
 		private function onViewOpinion(e:Event):void
 		{
 			//trace(view.dg1.selectedIndex);
 			if(opinionView == null) {
 				opinionView = PopUpManager.createPopUp(view.root, OpinionView, true) as OpinionView;
 				PopUpManager.centerPopUp(opinionView);
 			}
 			else {
 				PopUpManager.addPopUp(opinionView, view.root, true);
 			}
 			opinionView.setFocus();
 			opinionView.opinion = view.tsa.dataProvider.getItemAt(view.tsa.dga.selectedIndex).OPINION;
 		}
 		
 				
		private function onLoadCareer(proxy:CareerProxy):void
		{
			view.ts3.dataProvider = proxy.careerList;
		}
		
		private function onLoadFamily(proxy:FamilyProxy):void
		{
			view.ts5.dataProvider = proxy.familyList;			
		}
		
		private function onLoadScholarship(proxy:ScholarshipProxy):void
		{
			view.ts4.dataProvider = proxy.scholarshipList;
		}
		
 		private function onFailedDetail(proxy:ProfileDetailProxy):void
 		{
 			if(Variables.country!="CN") {
	 			view.ts1.dataProvider = new PrivateVO();
	 			view.ts5.dataProvider =  new ArrayCollection();
 			}
 			else {
	 			view.ts1cn.dataProvider = new PrivateVO();
	 			view.ts5cn.dataProvider =  new ArrayCollection();
	 			view.tsCn01.dataProvider = new ArrayCollection();
 			}
 			view.ts2.dataProvider = new ArrayCollection();
 			view.ts3.dataProvider = new ArrayCollection();
 			view.ts4.dataProvider = new ArrayCollection();
 			view.ts7.dataProvider = new ArrayCollection();
 			view.ts8.dataProvider = new ArrayCollection();
 			view.ts9.dataProvider = new ArrayCollection();
 			commonFacade.setStatusMessage("");
  			CommonAlert.showMessage(bundle,
 									"ALERT_FAILED_LOAD_PROFILE", [proxy.retCode],
 									"TITLE_INFO", view);
		}
 		


		private function onUpdatedPhones(proxy:PhonesProxy):void
		{
 			var profileProxy:ProfileProxy = facade.retrieveProxy(ProfileProxy.NAME) as ProfileProxy;
 			profileProxy.load(Variables.myPernr);
			if(Variables.country!="CN") {
				view.ts1.saveBtnFalse(true);
			}
			else {
				view.ts1cn.saveBtnFalse(true);
				
			}
			status = Constants.STATE_IDLE;
 			commonFacade.setStatusMessage("STATUS_UPDATED_PHONENUMBER");
		}
		

 		private function onFailedUpdatePhones(proxy:PhonesProxy):void
 		{
			if(Variables.country!="CN") {
				view.ts1.saveBtnFalse(true);
			}
			else {
				view.ts1cn.saveBtnFalse(true);
				
			}
  			CommonAlert.showMessage(bundle,
 									"ALERT_FAILED_UPDATE_PHONENUMBER", [proxy.retCode],
 									"TITLE_INFO", view);
 		}

		/*
		private function onUpdatedAddress(proxy:AddressProxy):void
		{
			setStatusMessage("현주소가 정상적으로 수정되었습니다.");
		}

		private function onFailedUpdateAddress(proxy:AddressProxy):void
		{
			AlertHelper.showMessage("현주소를 저장중 오류가 발생했습니다.", "DB 오류("+proxy.retCode+")");
		}
		*/










		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	private functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
 		/**
		 * @param
		 * @return void
		 */
		private function loadProfile():void
		{
 			var basicProxy:ProfileProxy = facade.retrieveProxy(ProfileProxy.NAME) as ProfileProxy;
 			basicProxy.load(Variables.myPernr);
 			
 			var detailProxy:ProfileDetailProxy = facade.retrieveProxy(ProfileDetailProxy.NAME) as ProfileDetailProxy;
			detailProxy.load(Variables.myPernr);
 			/*
 			var careerProxy:CareerProxy = facade.retrieveProxy(CareerProxy.NAME) as CareerProxy;
 			careerProxy.load(Variables.myPernr);
 			
 			var familyProxy:FamilyProxy = facade.retrieveProxy(FamilyProxy.NAME) as FamilyProxy;
 			familyProxy.load(Variables.myPernr);

 			var scholarshipProxy:ScholarshipProxy = facade.retrieveProxy(ScholarshipProxy.NAME) as ScholarshipProxy;
 			scholarshipProxy.load(Variables.myPernr);
			*/
 			status = Constants.STATE_LOADING_DATA;
		}


		/**
		 * @param
		 * @return void
		 */
 		private function tab1Init():void{
 			if(Variables.country != "CN") {
	 			view.ts1.currentState = "";
	 			view.ts1.vsTelOffice.selectedIndex = 0;
	 			view.ts1.vsFax.selectedIndex = 0;
	 			view.ts1.vsMobile1.selectedIndex = 0;
	 			view.ts1.vsMobile2.selectedIndex = 0;
	 			view.ts1.vsMail.selectedIndex = 0;
	 		}
	 		else {
	 			view.ts1cn.vsTelOffice.selectedIndex = 0;
	 			view.ts1cn.vsFax.selectedIndex = 0;
	 			view.ts1cn.vsMobile1.selectedIndex = 0;
	 			view.ts1cn.vsMobile2.selectedIndex = 0;
	 			view.ts1cn.vsMail.selectedIndex = 0;    
	 		}
	 		
	 		//0518
	 		// 사번이 14로 시작할경우는 다보이고 .. 아닐경우는 개인신상과 가족사항만 보이도록한다.
	 		//var s:String = "142423";
//	 		if(Variables.company == 'SAC' ) {
//		 		if(Variables.myPernr.indexOf('14') == 0) {
//		 		//if(s.indexOf('14') == 0 ) {
//		 			view.bAce14Check = true;	 			
//		 		} else {
//		 			view.bAce14Check = false;
//		 		}
//		 	}
 		}

		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event handler (from View, Internal or....)
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
  		private function onTabChange(event:Event):void{
 			tab1Init();
 		}
 		
 		private function mainDataReflesh(event:Event):void{
 			loadProfile();
 		}

		private function onChangedPhoneNumber(event:Event):void
		{		
			var phoneProxy:PhonesProxy = facade.retrieveProxy(PhonesProxy.NAME) as PhonesProxy;
			if(phoneProxy==null) {
				phoneProxy = new PhonesProxy();
				facade.registerProxy(phoneProxy);
			}
			
			var vo:PhonesVO = new PhonesVO();
			if(Variables.country!="CN") {
				vo.TEL_HOME 	= view.ts1.dataProvider.TEL_HOME;
				vo.TEL_OFFICE	= view.ts1.dataProvider.TEL_OFFICE;
				vo.TEL_MOBILE1	= view.ts1.dataProvider.TEL_MOBILE1;
				vo.TEL_MOBILE2	= view.ts1.dataProvider.TEL_MOBILE2;
				vo.FAX 			= view.ts1.dataProvider.FAX;
				vo.EMAIL_EX		= view.ts1.dataProvider.EMAIL_EX;
			}
			else {
				vo.TEL_HOME 	= view.ts1cn.dataProvider.TEL_HOME;
				vo.TEL_OFFICE	= view.ts1cn.dataProvider.TEL_OFFICE;
				vo.TEL_MOBILE1	= view.ts1cn.dataProvider.TEL_MOBILE1;
				vo.TEL_MOBILE2	= view.ts1cn.dataProvider.TEL_MOBILE2;
				vo.FAX 			= view.ts1cn.dataProvider.FAX;
				vo.EMAIL_EX		= view.ts1cn.dataProvider.EMAIL_EX;
			}
			phoneProxy.save(Variables.myPernr, vo);
			_status_update = true;
 			commonFacade.setStatusMessage("STATUS_UPDATING_PHONENUMBER");
		}

		private function onChangeAddress(event:Event):void
		{
			//view.ts1.vwAddr.selectedIndex = 1;
			var addrMediator:AddressMediator = facade.retrieveMediator(AddressMediator.NAME) as AddressMediator;
 			if(!addrMediator){
 				if(Variables.country!="CN") {
					addrMediator = new AddressMediator(view.ts1.addrView);
 				}
 				else {
					addrMediator = new AddressMediator(view.ts1cn.addrView);
 				}
	 			facade.registerMediator(addrMediator);
 			}else{
 				addrMediator.initView();
 			}
		}
		
		private function onAddCareer(e:Event):void
		{
			view.ts3.dataProvider.addItem(new CareerVO());
		}

		private function onAddScholarship(e:Event):void
		{
			view.ts4.dataProvider.addItem(new ScholarshipVO());	
		}

		private function onAddFamily(e:Event):void
		{
			view.ts5.dataProvider.addItem(new FamilyVO());
		}
		/*
		private var viewCareer:CareerView = null;
		private function onAddCareer(e:Event):void
		{
			viewCareer  = PopUpManager.createPopUp(view, CareerView, true) as CareerView;
			PopUpManager.centerPopUp(viewCareer);	
		}


		private var viewScholar:ScholarshipView= null;
		private function onAddScholarship(e:Event):void
		{
			viewScholar  = PopUpManager.createPopUp(view, ScholarshipView, true) as ScholarshipView;
			PopUpManager.centerPopUp(viewScholar);	
		}

		private var viewFamily:FamilyView = null;
		private function onAddFamily(e:Event):void
		{
			viewFamily  = PopUpManager.createPopUp(view, FamilyView, true) as FamilyView;
			PopUpManager.centerPopUp(viewFamily);	
		}
		*/

		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Utility Functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
        private function get view():OverView
        {
            return viewComponent as OverView;
        }
	}
}